package com.unittec.zk.provider.auth.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Joiner;
import com.unittec.zk.provider.auth.entity.SysRolePermission;
import com.unittec.zk.provider.auth.mapper.SysRolePermissionMapper;
import com.unittec.zk.provider.root.ServiceRoot;

/**
 * @description 角色菜单Service
 * @author Jason
 * @date 2019/11/07
 */
@Service
public class SysRolePermissionService extends ServiceRoot<SysRolePermissionMapper, SysRolePermission> {

    /**
     * 角色权限设置
     *
     * @param roleId
     * @param permissionIds
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveRolePermission(String roleId, String permissionIds) {
        String lastPermissionIds = "";
        List<String> lastPermissionIdList = getMapper()
            .selectList(new QueryWrapper<SysRolePermission>().lambda().eq(SysRolePermission::getRoleId, roleId))
            .stream().map(sysRolePermission -> String.valueOf(sysRolePermission.getPermissionId()))
            .collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(lastPermissionIdList)) {
            lastPermissionIds = Joiner.on(",").join(lastPermissionIdList);
        }
        List<String> add = getDiff(lastPermissionIds, permissionIds);
        if (add != null && add.size() > 0) {
            List<SysRolePermission> list = new ArrayList<SysRolePermission>();
            for (String p : add) {
                if (StringUtils.isNotEmpty(p)) {
                    SysRolePermission rolepms = new SysRolePermission(roleId, p);
                    list.add(rolepms);
                }
            }
            this.saveBatch(list);
        }

        List<String> delete = getDiff(permissionIds, lastPermissionIds);
        if (delete != null && delete.size() > 0) {
            for (String permissionId : delete) {
                this.remove(new QueryWrapper<SysRolePermission>().lambda().eq(SysRolePermission::getRoleId, roleId)
                    .eq(SysRolePermission::getPermissionId, permissionId));
            }
        }
    }

    /**
     * 从diff中找出main中没有的元素
     *
     * @param main
     * @param diff
     * @return
     */
    private List<String> getDiff(String main, String diff) {
        if (StringUtils.isBlank(diff)) {
            return null;
        }
        if (StringUtils.isBlank(main)) {
            return Arrays.asList(diff.split(","));
        }
        String[] mainArr = main.split(",");
        String[] diffArr = diff.split(",");
        Map<String, Integer> map = new HashMap<>(mainArr.length);
        for (String string : mainArr) {
            map.put(string, 1);
        }
        List<String> res = new ArrayList<String>();
        for (String key : diffArr) {
            if (StringUtils.isNotEmpty(key) && !map.containsKey(key)) {
                res.add(key);
            }
        }
        return res;
    }

}
